package com.lipuwater.agent.ui.utils;

import android.app.Activity;
import android.content.Intent;

import com.lipuwater.agent.ui.activity.CityChooseActivity;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class CityChooseUtils {

    private static CityChooseUtils mCityUtils;
    //节点对象
    private Iterator iter;
    //xml的节点数据
    private String[] mCityLevel = null;
    //回调接口
    private CallBack mCallBack;
    //选择结果
    private List<String> mResultsList = new ArrayList<>();
    private List<String> mResultsCodeList = new ArrayList<>();
    //当前选择等级（级别）
    private int mCurrentLevel = 0;

    public static CityChooseUtils getInstance() {
        if (mCityUtils == null) {
            synchronized (CityChooseUtils.class) {//线程锁
                if (mCityUtils == null)
                    mCityUtils = new CityChooseUtils();
            }
        }
        return mCityUtils;
    }

    /**
     * 跳转到地址选择
     * 设置xml等级，有外及内
     */
    public void toCityChoose(Activity activity, String... cityLevel) {
        this.mCityLevel = cityLevel;
        Intent intent = new Intent(activity, CityChooseActivity.class);
        activity.startActivityForResult(intent, 1);
    }


    public String[] getCityLevel() {
        return this.mCityLevel;
    }


    public void setCallBack(CallBack callBack) {
        this.mCallBack = callBack;
    }

    public interface CallBack {
        void chooseResults(List<String> results, List<String> resultsCode);
    }


    /**
     * 以下是内部调用------------------------------------------------
     */

    public void setChooseResults(String chooseRes, String chooseCode, boolean results) {
        if (results && mCallBack != null) {
            mResultsList.add(chooseRes);
            mResultsCodeList.add(chooseCode);
            mCallBack.chooseResults(mResultsList, mResultsCodeList);
            releaseRes();
        } else {
            mResultsList.add(chooseRes);
            mResultsCodeList.add(chooseCode);
        }
    }


    public Iterator getIter() {
        return iter;
    }

    public void setIter(Iterator iter) {
        this.iter = iter;
    }

    public int getCurrentLevel() {
        return mCurrentLevel;
    }

    public void setCurrentLevel(int currentLevel) {
        if (mCurrentLevel > currentLevel) {
            mResultsList.remove(mResultsList.size() - 1);
            mResultsCodeList.remove(mResultsCodeList.size() - 1);
        }
        this.mCurrentLevel = currentLevel;
    }

    /**
     * 重置对象
     */
    public void releaseRes() {
        try {
            this.iter = null;
            this.mCallBack = null;
            this.mCityLevel = null;
            this.mResultsList.clear();
            this.mResultsCodeList.clear();
            this.mCurrentLevel = 0;
        } catch (Exception e) {
        }
    }


}